﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace WebNlogTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {


            _logger.LogInformation("你好，者是日志");
            try
            {
                ttt();
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "报错了。请查看");
            }



            //循环插入5万条日志
            //for (int i=0;i<30000;i++)
            //{
            //    UserInfo user = new UserInfo();
            //    user.UserName = "bmm测试";
            //    long ts = ConvertDateTimeToInt(DateTime.Now);
            //    user.OrderNo = "SO"+ts.ToString();
            //    user.OrderType = 0;
            //    var strUser = JsonConvert.SerializeObject(user);
            //    _logger.LogInformation(String.Format("创建了订单{0}", strUser));

            //    _logger.LogError(new Exception("发生了一场"), strUser);

            //}


 
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            //long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            long t = (time.Ticks - 621356256000000000) / 10000;
            return t;
        }

        private void ttt()
        {
            int a = 10;
            int b = 0;
            int c = a / b;

        }
    }
}
